$(function () {
    //常用jQuery对象
    var entity_datagrid = $("#entity_list");
    var entity_dailog = $("#entity_dialog");
    var entity_form = $("#entity_form");


    //==========================================================================
    //封装方法
    var methodObj = {
        //添加按钮方法
        add: function () {
            $("#entity_form input").prop("disabled", false);
            $("#entity_dialog_btns").show();
            entity_form.form("clear");
            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "考试新增");
        },

        //编辑按钮方法
        edit: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            //回显联系人,先判断client是否为空
            if (row.client) {
                row["client.id"] = row.client.id;
                row["qq"] = row.client.qq;
                row["tel"] = row.client.tel;
                // test["clz"] = test.client.clz;
            }

            //回显处理人,先判断handler是否为空
            if (row.handler) {
                row["handler.id"] = row.handler.id;
            }

            entity_form.form("clear");
            $(".easyui-combobox").combobox({
                disabled: false
            });
            $(".easyui-datebox").datebox({
                disabled: false
            });
            $("#entity_form input").prop("disabled", false);

            $("#entity_dialog_btns").show();

            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "编辑");
            $("#cc1").combobox({
                readonly:true
            })
            //回显列表数据
            entity_dailog.form("load", row);

        },


        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            entity_form.form("submit", {
                url: "/test/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_dailog.dialog("close");
                            entity_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_dailog.dialog("close");
                        });
                    }
                }
            })
        },

        delete: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示', "您确定要删除该考试信息么?", function (r) {
                if (r) {
                    $.get("/test/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                entity_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });

        },


        //设置考试状态
        changState: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            var entity_state = "考试通过？";
            if (row.customerStatus) {
                var entity_state = "取消考试通过？";
            }
            $.messager.confirm('温馨提示', entity_state, function (r) {
                if (r) {
                    $.get("/test/changState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                entity_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });

        },
        //高级查询
        searchs: function () {
            var clientId = $("#clientId").combobox("getValue");
            entity_datagrid.datagrid("load", {
                clientId: clientId,
            });
        },

        //刷新页面
        reloadList: function () {
            entity_datagrid.datagrid("reload");
        },

        //查看
        see: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            //回显联系人,先判断client是否为空
            if (row.client) {
                row["client.id"] = row.client.id;
                row["qq"] = row.client.qq;
                row["tel"] = row.client.tel;
                // test["clz"] = test.client.clz;
            }

            //回显处理人,先判断handler是否为空
            if (row.handler) {
                row["handler.id"] = row.handler.id;
            }


            $("#entity_form input").prop("disabled", true);
            $(".easyui-combobox").combobox({
                disabled: true
            });
            $(".easyui-datebox").datebox({
                disabled: true
            });
            $("#entity_dialog_btns").hide();


            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "编辑");

            $("#cc1").combobox({
                readonly: true
            })

            //回显列表数据
            entity_dailog.form("load", row);

        },
        cancel: function () {
            entity_dailog.dialog("close");
        },
    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    //页面数据加载
    entity_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/test/query.do",
        pagination: true,
        singleSelect: true,
        nowrap: false,
        toolbar: "#tools",
        onClickRow: function (index, row) {
            if (row.testResult) {
                $("#btn_changeState").linkbutton({
                    text: "取消通过"
                });
                $("a[data-cmd='edit']").linkbutton("disable");//编辑 disable
            } else {
                $("#btn_changeState").linkbutton({
                    text: "通过"
                });
                $("a[data-cmd='edit']").linkbutton("enable");
            }

        },
        columns: [[
            {field: 'id', title: '考试编号', width: 50, align: 'center'},
            {
                field: 'clientName',
                title: '姓名',
                width: 50,
                align: 'center',
                formatter: function (value, row, index) {
                    return row.client ? row.client.name : "";
                }
            },
            {
                field: 'clientSalemanUsername',
                title: '营销人员',
                width: 50,
                align: 'center',
                formatter: function (value, row, index) {
                    return row.client ? (row.client.saleman ? row.client.saleman.username : "") : "";
                }
            },
            {
                field: 'clientQQ',
                title: 'QQ',
                width: 50,
                align: 'center',
                formatter: function (value, row, index) {
                    return row.client ? row.client.qq : "";
                }
            },
            {
                field: 'clientTel',
                title: '电话',
                width: 50,
                align: 'center',
                formatter: function (value, row, index) {
                    return row.client ? row.client.tel : "";
                }
            },
            {
                field: 'clientClzName',
                title: '意向班级',
                width: 50,
                align: 'center',
                formatter: function (value, row, index) {
                    return row.client ? (row.client.clz ? row.client.clz.name : "") : "";
                }
            },

            {field: 'testDate', title: '考试时间', width: 50, align: 'center'},
            {
                field: 'testResult',
                title: '考试结果',
                width: 50,
                align: 'center',
                formatter: function (value, row, index) {
                    if (value == null) {
                        return "";
                    } else {
                        return value ? "<font color='green'>通过</font>" : "<font color='red'>不通过</font>";
                    }
                }
            },
            {field: 'remark', title: '备注', width: 50, align: 'center'},
            {
                field: 'handler',
                title: '处理人',
                width: 50,
                align: 'center',
                formatter: function (value, row, index) {
                    return row.handler ? row.handler.username : "";
                }
            },


        ]],


    });

    //编辑添加弹出框设置
    entity_dailog.dialog({
        width: 400,
        height: 400,
        closed: true,
        buttons: "#entity_dialog_btns",
        onClose: function () {
            $(".easyui-combobox").combobox({
                disabled: false
            });
            $(".easyui-datebox").datebox({
                disabled: false
            });
            $("#entity_form input").prop("disabled", false);
            $("#cc1").combobox({
                readonly:false
            })
        }
    });

    $("#cc1").combobox({
        valueField: 'id',
        textField: 'name',
        url: '/client/listAll.do',
        onSelect: function (rec) {
            $("#cc2").val(rec.qq);
            $("#cc3").val(rec.tel);
            //$("#cc4").val(rec.clz);
        }
    });


    //form表单设置
    entity_dailog.form({
        fit: true
    });

});

